// var DB = require("../lib/db_pg").DB;
var DB = require("../lib/db_mysql").DB;
var config = require('../lib/config');
var resErr = require('../lib/util.js').resErr;
var md5 = require('../lib/util.js').md5;
var async = require('async');
var http_request = require('../lib/util.js').http_request;

exports.class_add = function(req, res){
    var dept_id = req.param('department_id');
    if(!dept_id) return res.json({success:false,msg:'院系id不能为空！'});

    var class_name = req.param('class_name');
    if(!class_name) return res.json({success:false,msg:'班级名称不能为空！'});

    var teacher_id = req.param('teacher_id'); // 班级老师id可为空
    if(!teacher_id) teacher_id = 0;

    var db = new DB();
    
    async.waterfall([
        function(cb){
            var tmp_params = [];
            var sql_text = 'select count(1) as counter from t_class where class_name = ?;';
            tmp_params.push(class_name);
            db.query(sql_text,tmp_params,function(err,datas){
                if(err) return cb(err);
                if(datas.length > 0 && datas[0].counter > 0){
                  return cb('该班级已经存在！');
                }
                return cb(null);
            });
        }, function(cb){
            var tmp_params = [];
            var sql_text = 'insert into t_class(department_id, class_name, teacher_id, enable_flag) values(?, ?,?,"Y");';
            tmp_params.push(dept_id);
            tmp_params.push(class_name);
            tmp_params.push(teacher_id);
            db.query(sql_text, tmp_params, function(err, data){
                console.log(err);
                console.log(data);
                if(err){
                    return cb(err);
                }
                return cb(null);
            });
        }
    ], function(err){ // callback
        if (err){
            return res.json({success:false,msg:JSON.stringify(err)});
        }      
        return res.json({success:true,msg:'班级添加成功！'});
    });
};

exports.class_del = function(req, res){
    var class_id = req.param('class_id');
    if(!class_id) return res.json({success: false, msg: '班级代码不能为空'});
    
    var db = new DB();
    var sql_text = 'delete from t_class where class_id = ?;';
    
    var tmp_params = [];
    tmp_params.push(class_id);
    
    db.query(sql_text, tmp_params, function(err, datas){
        console.log(datas);
        if(err){
            return resErr(res, err);
        }
        return res.json({success: true, msg: '班级删除成功'});
    });
}

exports.class_upd = function(req, res){
    var class_id = req.param('class_id');
    if(!class_id) return res.json({success: false, msg: '班级代码不能为空'});
    var class_name = req.param('class_name');
    if(!class_name) return res.json({success: false, msg: '班级名称不能为空'});
    var teacher_id = req.param('teacher_id');
    if(!teacher_id) teacher_id = '';
    
    var db = new DB();
    var sql_text = 'update t_class set class_name = ?, teacher_id = ? ';
    sql_text += ' where class_id = ?;';
    
    var tmp_params = [];
    tmp_params.push(class_name);
    tmp_params.push(teacher_id);
    tmp_params.push(class_id);
    
    db.query(sql_text, tmp_params, function(err, datas){
        console.log(datas);
        if(err){
            return resErr(res, err);
        }
        return res.json({success: true, msg: '班级修改成功'});
    });
}